package leetcode.primary;

public class T283 {
    public void moveZeroes(int[] nums) {
        int count = 0;
        int left=0;
        int right;
        for ( right=0;right< nums.length;right++) {
            if (nums[right]==0){
                count++;
                move(count-1,nums,left,right);
                System.out.println("left="+left+",right="+right+",count="+(count-1));

                left=right+1;
                while (left< nums.length && nums[left]==0){
                    left++;
                }
                right = left;
            }
        }
        if (left!=right){
            move(count,nums,left,right);
        }
        for (int i = nums.length-1; i>nums.length-1-count ; i--) {
            nums[i]=0;
        }

    }

    public void move(int n,int[] nums,int left,int right){
        for (int i = left; i <right ; i++) {
            if (i-n>=0){
                nums[i-n] = nums[i];
            }
        }
    }

    public static void main(String[] args) {
        T283 t283 = new T283();
        int[] nums = {0,0,0,3,12};
        t283.moveZeroes(nums);
        for (int num : nums) {
            System.out.print(num+" ");
        }

    }
}
